package Secondpagh;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import SHU.water2.R;
import SHU.water2.Spinnerstyle;

import static SHU.water2.loginin.connection;
import static SHU.water2.loginin.power;

public class abook extends AppCompatActivity implements View.OnClickListener {
    public ArrayList L1 = new ArrayList();
    public ArrayList L2 = new ArrayList();
    public ArrayList L3 = new ArrayList();
    public ArrayList L4 = new ArrayList();
    public Spinner spinner1;
    public Spinner spinner2;
    public ArrayAdapter<String> arr_adapter1;
    public ArrayAdapter<String> arr_adapter2;

    public boolean getdata = false;
    public List<HashMap<String, Object>> data;
    private Handler handler;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abook);
        ImmersionBar.with(this).statusBarColor(R.color.color).fitsSystemWindows(true).init();
        handler = new Handler() {
            //接收消息
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                int flag = (int) msg.obj;
                switch (flag) {
                    /*case 1:
                        spinerset();
                        break;*/
                }

            }
        };
        if (power == 1) {
            Button B = findViewById(R.id.add1);
            B.setVisibility(View.INVISIBLE);
        }

        spinerset();
        click();
        connect();
    }

    private void connect() {
        //新建线程连接数据库，防止主界面卡死
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    /*Class.forName("com.mysql.jdbc.Driver");
                    String url = "jdbc:mysql://81.68.81.12:3306/jinshan";
                    String name = "rkl";
                    String passwd = "123456";
                    Connection connection = DriverManager.getConnection(url, name, passwd);
                    System.out.println("连接成功");*/

                    String sql = "select * from a_book";
                    try {
                        // 创建用来执行sql语句的对象
                        Statement statement = connection.createStatement();
                        // 执行sql查询语句并获取查询信息
                        ResultSet rSet = statement.executeQuery(sql);

                        while (rSet.next()) {
                            L1.add(rSet.getString("name"));
                            L2.add(rSet.getString("phone"));
                            L3.add(rSet.getString("address"));
                            L4.add(rSet.getString("department"));
                        }


                        data = new ArrayList<HashMap<String, Object>>();

                        for (int i = 0; i < L1.size(); i++) {
                            HashMap<String, Object> item = new HashMap<String, Object>();
                            item.put("name", L1.get(i));
                            item.put("phone", L2.get(i));
                            item.put("part", L3.get(i));
                            item.put("apart", L4.get(i));
                            data.add(item);
                        }


                        if (L1 != null)
                            getdata = true;

                    } /*catch (SQLException e) {
                        Log.e("abook", "查询错误");
                    }*/


                    /*connection.close();
                    System.out.println("连接关闭");*/ finally {

                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }


    public void setview() {

        System.out.println(data);

        //创建SimpleAdapter适配器将数据绑定到item显示控件上
        SimpleAdapter adapter = new SimpleAdapter(abook.this, data, R.layout.abooklist,
                new String[]{"name", "part", "apart", "phone"}, new int[]{R.id.name, R.id.part, R.id.apart, R.id.phone});

        ListView listView1 = (ListView) findViewById(R.id.list1);
        listView1.setAdapter(adapter);

    }

    private void click() {
        findViewById(R.id.ret1).setOnClickListener(this);
        findViewById(R.id.check1).setOnClickListener(this);
        findViewById(R.id.add1).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ret1:
                finish();
                break;

            case R.id.check1:
                if (getdata == false) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("尚未读回数据，请刷新重试").setPositiveButton("是", null).show();
                } else
                    setview();
                break;
            case R.id.add1:
                //实例化
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                //定义弹框内容
                AlertDialog alertDialog = builder.create();
                View view = View.inflate(getApplicationContext(), R.layout.abookadd, null);
                alertDialog.setView(view);
                alertDialog.show();

                EditText zhandian = view.findViewById(R.id.edit1);


                view.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                view.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();

                    }
                });
        }
    }

    public void spinerset() {


        spinner1 = findViewById(R.id.spinner1);
        spinner2 = findViewById(R.id.spinner2);
        String[] items1 = getResources().getStringArray(R.array.abook1);
        String[] items2 = getResources().getStringArray(R.array.abook2);


        //适配器1
        arr_adapter1 = new Spinnerstyle(this, items1);
        //设置样式
        arr_adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner1.setAdapter(arr_adapter1);

        //适配器2
        arr_adapter2 = new Spinnerstyle(this, items2);
        //设置样式
        arr_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner2.setAdapter(arr_adapter2);

    }
}